package com.sicau.socialpracticesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sicau.socialpracticesystem.common.myenum.TeamMemberRoleEnum;
import com.sicau.socialpracticesystem.common.myenum.TeamMemberStateEnum;
import com.sicau.socialpracticesystem.entity.param.PageParam;
import com.sicau.socialpracticesystem.entity.param.teammember.TeamApplyParam;
import com.sicau.socialpracticesystem.entity.param.teammember.TeamApplyQueryParam;
import com.sicau.socialpracticesystem.entity.po.TeamMember;
import com.sicau.socialpracticesystem.entity.vo.PageDataVO;
import com.sicau.socialpracticesystem.entity.vo.team.TeamVO;
import com.sicau.socialpracticesystem.entity.vo.teammember.TeamMemberVO;
import com.sicau.socialpracticesystem.mapper.TeamMemberMapper;
import com.sicau.socialpracticesystem.mapper.VOMapper;
import com.sicau.socialpracticesystem.service.TeamMemberService;
import com.sicau.socialpracticesystem.service.TeamService;
import com.sicau.socialpracticesystem.util.PageUtil;
import com.sicau.socialpracticesystem.util.UserContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.ArrayList;
import java.util.Objects;

/**
 * <p>
 * 团队成员信息表 服务实现类
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-04-26
 */
@Service
public class TeamMemberServiceImpl extends ServiceImpl<TeamMemberMapper, TeamMember> implements TeamMemberService {

    @Resource
    TeamMemberMapper teamMemberMapper;

    @Resource
    TeamService teamService;

    @Resource
    private VOMapper.TeamMemberVOMapper teamMemberVOMapper;

    @Override
    public TeamMember queryByTeamIdAndUserId(Integer teamId, Integer userId) {
        if (teamId == null || userId == null) {
            return null;
        }
        LambdaQueryWrapper<TeamMember> queryWrapper = new LambdaQueryWrapper<TeamMember>()
                .eq(TeamMember::getTeamId, teamId)
                .eq(TeamMember::getUserId, userId)
                .eq(TeamMember::getState, TeamMemberStateEnum.PASSED.getState());
        return teamMemberMapper.selectOne(queryWrapper);
    }

    @Override
    public PageDataVO<TeamMemberVO> queryTeamMemberByTeamId(Integer teamId, PageParam pageParam) {
        Page<TeamMemberVO> teamMemberVOPage = teamMemberVOMapper.selectPage(
                new Page<>(pageParam.getCurrentPage(), pageParam.getPageSize()),
                new LambdaQueryWrapper<TeamMemberVO>().eq(TeamMemberVO::getTeamId, teamId));
        return PageUtil.convert(teamMemberVOPage);
    }

    @Override
    public List<TeamMemberVO> queryTeamMemberByTeamId(Integer teamId) {
        return teamMemberVOMapper.selectList(new LambdaQueryWrapper<TeamMemberVO>()
                .eq(TeamMemberVO::getTeamId, teamId)
                .eq(TeamMemberVO::getState, TeamMemberStateEnum.PASSED.getState())
        );
    }

    @Override
    public PageDataVO<TeamMemberVO> getTeamsApplyMembers(TeamApplyQueryParam teamApplyQueryParam) {
        List<TeamVO> teamVOList = teamService.queryMyLeadingTeam();
        if (teamVOList == null || teamVOList.isEmpty()) {
            return null;
        }
        ArrayList<Integer> teamIdList = new ArrayList<>();
        teamVOList.forEach(item -> teamIdList.add(item.getId()));
        Page<TeamMemberVO> page = new Page<>(teamApplyQueryParam.getCurrentPage(), teamApplyQueryParam.getPageSize());

        LambdaQueryWrapper<TeamMemberVO> queryWrapper = new LambdaQueryWrapper<TeamMemberVO>()
                .eq(TeamMemberVO::getState, teamApplyQueryParam.getState())
                .ne(TeamMemberVO::getMemberRole, TeamMemberRoleEnum.CAPTAIN)
                .in(TeamMemberVO::getTeamId, teamIdList);

        return PageUtil.convert(teamMemberVOMapper.selectPage(page, queryWrapper));
    }

    @Override
    public void applyTeam(TeamApplyParam teamApplyParam) {

        LambdaQueryWrapper<TeamMember> queryWrapper = new LambdaQueryWrapper<TeamMember>()
                .eq(TeamMember::getUserId, UserContext.get().getId())
                .eq(TeamMember::getTeamId, teamApplyParam.getTeamId());
        TeamMember teamMember = teamMemberMapper.selectOne(queryWrapper);
        if (teamMember != null){
            switch (Objects.requireNonNull(TeamMemberStateEnum.getEnum(teamMember.getState()))){
                case PASSED:
                case HAS_APPLIED:
                    return;
                case DENIED:
                    teamMember.setState(TeamMemberStateEnum.HAS_APPLIED.getState());
                    teamMemberMapper.updateById(teamMember);
            }
        }else {
            teamMember = new TeamMember();
            teamMember.setTeamId(teamApplyParam.getTeamId());
            teamMember.setUserId(UserContext.get().getId());
            teamMember.setRole(TeamMemberRoleEnum.MEMBER.getRole());
            teamMember.setState(TeamMemberStateEnum.HAS_APPLIED.getState());
            teamMemberMapper.insert(teamMember);
        }

    }
}
